package cn.work.dao;

import cn.work.bean.serviceclassbean;
import cn.work.util.DataBaseutil;
import jdk.jfr.Category;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class serviceclassDao {

//    插入数据
    public boolean insertCategory(String className) {
        String sql = "INSERT INTO service_class (serviceclass_name) VALUES (?)";
        try (Connection conn = DataBaseutil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, className);
            return stmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public List<serviceclassbean> getAllCategories() {
        List<serviceclassbean> categories = new ArrayList<>();
        String sql = "SELECT * FROM service_class";  // 保持原来的查询

        try (Connection conn = DataBaseutil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {

            while (rs.next()) {
                // 根据数据库列名获取数据
                int id = rs.getInt("serviceclass_id");  // 使用正确的列名
                String name = rs.getString("serviceclass_name");  // 使用正确的列名

                // 创建并填充 serviceclassbean 对象
                serviceclassbean category = new serviceclassbean();
                category.setServiceclass_id(id);    // 设置 ID
                category.setServiceclass_name(name); // 设置分类名称

                // 将分类数据加入列表
                categories.add(category);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return categories;
    }

}
